<template>
  <div class="search">
    <Card>
      <Row @keydown.enter.native="handleSearch">
        <Form
          ref="searchForm"
          :model="searchForm"
          inline
          :label-width="70"
          class="search-form"
          @submit.native.prevent
        >
          <Form-item label="圈子名称" prop="keywords">
            <Input
              type="text"
              v-model="searchForm.keywords"
              placeholder="请输入圈子名称"
              clearable
              style="width: 200px"
            />
          </Form-item>
          <Button
            @click="handleSearch"
            class="search-btn"
            type="primary"
            icon="ios-search"
            >搜索</Button
          >
        </Form>
      </Row>
      <Row class="operation padding-row">
        <Button @click="handleAdd" type="primary">添加</Button>
        <Button @click="batchDel" type="primary">删除</Button>
      </Row>
      <Table
        :loading="loading"
        border
        :columns="columns"
        sortable="custom"
        class="mt_10"
        :data="data"
        ref="table"
        @on-selection-change="changeSelect"
      ></Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.page"
          :total="total"
          :page-size="searchForm.pagesize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
  </div>
</template>

<script>
import * as API_community from "@/api/community.js";

export default {
  name: "field-list",
  data() {
    return {
      loading: false, // 加载状态
      searchForm: {
        // 请求参数
        page: 1,
        pagesize: 10,
        keywords: null, //圈子名称
      },
      selectList: [], // 多选数据
      columns: [
        // 表头
        { type: "selection", width: 60, align: "center" },
        { title: "序号", width: 100, type: "index" },
        { title: "圈子名称", key: "title", minWidth: 160, tooltip: true },
        {
          title: "圈子封面",
          minWidth: 120,
          render: (h, params) => {
            return h("img", {
              attrs: { src: params.row.thumb },
              style: { width: "100px" },
            });
          },
        },
        { title: "圈子成员数量", key: "member_focus", minWidth: 120, tooltip: true },
        { title: "发帖数量", key: "topiclist", minWidth: 100, tooltip: true },
        {
          title: "创建时间",
          minWidth: 180,
          tooltip: true,
          render: (h, params) => {
            return h("span", {}, this.format(params.row.ctime));
          },
        },
        {
          title: "置顶",
          align: "center",
          minWidth: 120,
          render: (h, params) => {
            let label = params.row.top == 0 ? "置顶" : "取消置顶";
            return this.btnDom(h, null, label, "handleTop", params);
          },
        },
        {
          title: "操作",
          align: "center",
          minWidth: 120,
          fixed: "right",
          render: (h, params) => {
            return h("div", { style: { display: "flex", justifyContent: "center" } }, [
              this.btnDom(h, "primary", "编辑", "handleEdit", params),
              this.btnDom(h, "error", "删除", "handleDel", params),
            ]);
          },
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
    };
  },
  methods: {
    // 分页 改变页码
    changePage(v) {
      this.searchForm.page = v;
      this.getData();
    },
    // 分页 改变页数
    changePageSize(v) {
      this.searchForm.pagesize = v;
      this.searchForm.page = 1;
      this.getData();
    },
    // 搜索
    handleSearch() {
      this.searchForm.page = 1;
      this.getData();
    },
    //多选
    changeSelect(e) {
      this.selectList = e;
    },
    // 时间戳转换
    format(v) {
      let date = "";
      if (v) date = new Date(v * 1000).Format("yyyy-MM-dd hh:mm:ss");
      return date;
    },
    // 按钮
    btnDom(h, type = null, label = "", method, params, ghost = false) {
      return h(
        "Button",
        {
          props: { type, size: "small", ghost },
          style: { marginRight: "5px" },
          on: {
            click: () => {
              this[method](params.row);
            },
          },
        },
        label
      );
    },
    // 新增
    handleAdd() {
      this.$router.push({
        name: "field-form",
        query: { id: null },
      });
    },
    //修改
    handleEdit(v) {
      this.$router.push({
        name: "field-form",
        query: { id: v.id },
      });
    },
    // 置顶
    handleTop(row) {
      let label = row.top == 0 ? "置顶" : "取消置顶";
      this.$Modal.confirm({
        title: label,
        content: "<p>确认是否" + label + "吗？</p>",
        onOk: () => {
          let params = {
            id: row.id,
            top: row.top == 0 ? 1 : 0,
          };
          API_community.circleTop(params).then((res) => {
            if (res.code == 200) {
              this.$Message.success(`${label}成功`);
              this.getData();
            }
          });
        },
      });
    },
    // 批量删除
    batchDel() {
      if (this.selectList.length) {
        this.$Modal.confirm({
          title: "确认删除",
          content: "<p>您确认要删除所选的" + this.selectList.length + "条圈子吗？</p>",
          onOk: () => {
            let ids = this.selectList
              .map((i) => {
                return i.id;
              })
              .join(",");
            API_community.circleDel({ ids }).then((res) => {
              if (res.code == 200) {
                this.$Message.success(res.msg);
                this.getData();
              }
            });
          },
        });
      } else {
        this.$Message.warning("请先勾选数据");
      }
    },
    // 删除
    handleDel(v) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "<p>您确认要删除本条圈子？</p>",
        onOk: () => {
          API_community.circleDel({ ids: v.id }).then((res) => {
            if (res.code == 200) {
              this.$Message.success(res.msg);
              this.getData();
            }
          });
        },
      });
    },
    //查询圈子列表
    getData() {
      this.loading = true;
      API_community.circleList(this.searchForm)
        .then((res) => {
          if (res.code == 200) {
            this.data = res.data;
            this.total = res.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
  mounted() {
    this.getData();
  },
};
</script>
<style lang="scss" scoped>
::v-deep .ivu-table-wrapper {
  width: 100%;
}
::v-deep .ivu-card {
  width: 100%;
}
.face {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
</style>
